package com.sunma.assetsin.service.impl;

import com.sunma.assets.domain.Assets;
import com.sunma.assets.service.IAssetsService;
import com.sunma.assetsin.mapper.AssetsInMapper;
import com.sunma.assetsin.domain.AssetsIn;
import com.sunma.assetsin.service.IAssetsInService;
import com.sunma.common.core.domain.AjaxResult;
import com.sunma.common.core.domain.entity.SysUser;
import com.sunma.common.core.domain.model.LoginUser;
import com.sunma.common.exception.ServiceException;
import com.sunma.common.utils.DateUtils;
import com.sunma.common.utils.SecurityUtils;
import com.sunma.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 资产入库Service业务层处理
 *
 * @author sunma
 * @date 2024-07-10
 */
@Service
public class AssetsInServiceImpl implements IAssetsInService {
    @Autowired
    private AssetsInMapper assetsInMapper;

    @Resource
    private IAssetsService assetsService;

    /**
     * 查询资产入库
     *
     * @param id 资产入库主键
     * @return 资产入库
     */
    @Override
    public AssetsIn selectAssetsInById(Long id) {
        return assetsInMapper.selectAssetsInById(id);
    }

    /**
     * 查询资产入库列表
     *
     * @param assetsIn 资产入库
     * @return 资产入库
     */
    @Override
    public List<AssetsIn> selectAssetsInList(AssetsIn assetsIn) {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser)) {
            SysUser currentUser = loginUser.getUser();
            // 如果是超级管理员，则不过滤数据
            if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin()) {
                assetsIn.setUserId(currentUser.getUserId());
            }
        }
        return assetsInMapper.selectAssetsInList(assetsIn);
    }

    /**
     * 新增资产入库
     *
     * @param assetsIn 资产入库
     * @return 结果
     */
    @Override
    public int insertAssetsIn(AssetsIn assetsIn) {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser)) {
            SysUser currentUser = loginUser.getUser();
            if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin()) {

                assetsIn.setUserId(currentUser.getUserId());
            } else {
                // 抛出异常
                throw new ServiceException("不能以管理员权限进行入库操作");
            }
        }

        assetsIn.setDate(DateUtils.getNowDate());
        assetsIn.setStatus(String.valueOf(0));

        return assetsInMapper.insertAssetsIn(assetsIn);
    }

    /**
     * 修改资产入库
     *
     * @param assetsIn 资产入库
     * @return 结果
     */
    @Override
    public int updateAssetsIn(AssetsIn assetsIn) {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser currentUser = null;

        if (loginUser != null) {
            currentUser = loginUser.getUser();

            // 检查是否需要扣减库存
            if ("1".equals(assetsIn.getStatus())) // 这里可以根据你的业务逻辑调整条件
            {
                Assets assets = assetsService.selectAssetsById(assetsIn.getAssetsId());
                assets.setNum(assets.getNum() + assetsIn.getNum());

                // 尝试更新库存，这里可能需要乐观锁机制防止并发更新问题
                int updated = assetsService.updateAssets(assets);
                if (updated < 0) {

                    throw new ServiceException("资产入库失败了！");

                } else {
                    AjaxResult.success("资产已成功入库！");
                }
            }
        }
        return assetsInMapper.updateAssetsIn(assetsIn);
    }

    /**
     * 批量删除资产入库
     *
     * @param ids 需要删除的资产入库主键
     * @return 结果
     */
    @Override
    public int deleteAssetsInByIds(Long[] ids) {
        return assetsInMapper.deleteAssetsInByIds(ids);
    }

    /**
     * 删除资产入库信息
     *
     * @param id 资产入库主键
     * @return 结果
     */
    @Override
    public int deleteAssetsInById(Long id) {
        return assetsInMapper.deleteAssetsInById(id);
    }
}
